<?php
class products extends CWidget
{
	public $_catID = null;
	public $_data = null;
	public $_paging = null;
	public $_itemOnPage = 5;
	public $_currPage = null;
	public $_total = 0;
	public $_order = '';
	public function init()
	{
		parent::init();
	}
	public function run()
	{
		if($this->_data==null){
			$order = "lastest";
			$orderQuery="";
			if(isset($_POST['Products'])){
				$order = $_POST['Products']['order'];
				switch ($order){
					case 'hight':
						$orderQuery = "ORDER BY price DESC";
						break;
					case 'low':
						$orderQuery = "ORDER BY price ASC";
						break;
					default:
						$orderQuery = "";
						break;
				}
				
			}
			$total = FrontendShopProductsModel::getTotalProductByCat($this->_catID);
			$page = new CPagination($total);
			$itemOnPaging = 5;
			$page->pageSize = Yii::app()->params['perpage'];
			$curr_page = $page->getCurrentPage();
			$data = FrontendShopProductsModel::getProductByCat($this->_catID, $page->getLimit(),$page->getOffset(), $orderQuery);
			$this->render('default', array(
					'data' =>$data,
					'page'=>$page,
					'itemOnPaging'	=>	$itemOnPaging,
					'curr_page'	=>	$curr_page,
					'total'=>$total,
					'order'=>$order
			));
		}else{
			$this->render('default', array(
					'data' =>$this->_data,
					'page'=>$this->_paging,
					'itemOnPaging'	=>	$this->_itemOnPage,
					'curr_page'	=>	$this->_currPage,
					'total'=>$this->_total,
					'order'=>$this->_order
			));
		}
		
	}
}